package com.xmcowell.sportpacemaster.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.xmcowell.sportpacemaster.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanyingmao on 17/1/5.
 */

public class SmoothView extends View {
    private Paint mPaint, mPaint2;//圆弧画笔
    public static final int COLOR = 0xFFA6D4AE;//外环的颜色
    private ArrayList<Integer> listSpeed;
    private ArrayList<Point> listPoint;
    private int widthSize, heightSize;
    private int totalSize;
    private List<Float> res;
    private float pro = 0;
    private Bitmap arrow;
    float[] ptst;
    private int line_width = 5;

    private boolean bg = true;
    private float currentY = 0;

    public SmoothView(Context context) {
        super(context);
    }

    public SmoothView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SmoothView);
        line_width = ta.getInteger(R.styleable.SmoothView_line_width, line_width);

        ta.recycle();

        mPaint = new Paint();
        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
//        mPaint.setColor(Color.parseColor("#ff9515"));
        mPaint.setColor(getResources().getColor(R.color.color_line));
        mPaint.setStrokeWidth(dip2px(line_width));


        mPaint2 = new Paint();
        mPaint2.setAntiAlias(true);
//        mPaint2.setColor(Color.parseColor("#666666"));
        mPaint2.setColor(getResources().getColor(R.color.color_666));
        mPaint2.setStrokeWidth(dip2px(1));
        mPaint2.setStyle(Paint.Style.FILL_AND_STROKE);

        arrow = BitmapFactory.decodeResource(context.getResources(),
                R.mipmap.icon_arrow2);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        widthSize = MeasureSpec.getSize(widthMeasureSpec) * 4 / 5;
        heightSize = MeasureSpec.getSize(heightMeasureSpec);// * 4 / 5;
        Log.e("SmoothView", "heightSize = " + heightSize);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //if(ptst == null){
        if (listSpeed == null)
            return;
        ptst = new float[listSpeed.size() * 2];
        for (int i = 0; i < listSpeed.size(); i++) {
            ptst[i * 2] = i * widthSize / listSpeed.size() + widthSize / 8;
            ptst[i * 2 + 1] = heightSize - (int) (listSpeed.get(i) * heightSize / totalSize);// + heightSize / 28;
        }
        //}
        res = drawSmooth(ptst, null, null, null);

        float x = widthSize * pro*(19.0f/20) + widthSize / 8;
        float y = 0;
        double argle = 0f;

        if (bg) {
            for (int i = 1; i < res.size() / 2; i++) {
                for (int j = 0; j < ptst.length / 2 - 1; j++) {
                    if (j % 2 == 0 && ptst[j * 2] <= res.get((i - 1) * 2) && res.get((i - 1) * 2) < ptst[(j + 1) * 2]) {
                        //画梯形
                        Path path = new Path();
                        path.moveTo(res.get((i - 1) * 2), res.get((i - 1) * 2 + 1));
                        path.lineTo(res.get((i - 1) * 2), heightSize * 6 / 5 + dip2px(3));
                        path.lineTo(res.get(i * 2), heightSize * 6 / 5 + dip2px(3));
                        path.lineTo(res.get(i * 2), res.get(i * 2 + 1));
                        canvas.drawPath(path, mPaint2);
                    }
                }
            }
        }

        for (int i = 1; i < res.size() / 2; i++) {
            int alpha = (res.size() / 2 - i - 1) * 5;
            if (alpha < 255) {
                mPaint.setAlpha(alpha);
            }
            canvas.drawLine(res.get((i - 1) * 2), res.get((i - 1) * 2 + 1), res.get(i * 2), res.get(i * 2 + 1), mPaint);

            if (res.get((i - 1) * 2) <= x && res.get(i * 2) > x) {
                argle = (res.get(i * 2 + 1) - res.get((i - 1) * 2 + 1)) / (res.get(i * 2) - res.get((i - 1) * 2));
                y = (res.get(i * 2 + 1) - res.get((i - 1) * 2 + 1)) / (res.get(i * 2) - res.get((i - 1) * 2)) * (x - res.get((i - 1) * 2)) + res.get((i - 1) * 2 + 1);
                argle = Math.atan(argle);
            }
        }

        Log.e("SmoothView", "y1 = " + y);
        if (y > 0) {
            currentY = y;
        } else {
            y = currentY;
        }
        Matrix matrix = new Matrix();
        float ar = (float) (argle / Math.PI * 180);
        matrix.postRotate(ar, arrow.getWidth() / 2, arrow.getHeight() / 2);
        x = x - arrow.getWidth() / 2;
        y = y - arrow.getHeight() / 2;
        matrix.postTranslate(x, y);
        Log.e("SmoothView", "y2 = " + y);
        mPaint.setAlpha(255);
        if (y > 0) {
            canvas.drawBitmap(arrow, matrix, mPaint);
        }
    }

    public void setPro(float pro) {
        this.pro = pro;
        invalidate();
    }

    public void setListSpeed(ArrayList<Integer> listSpeed, int totalSize) {
        for (Integer i : listSpeed) {
            Log.e("SmoothView", "item = " + i);
        }
        Log.e("SmoothView", "totalSize = " + totalSize);
        this.listSpeed = listSpeed;
        this.totalSize = totalSize;
        invalidate();
    }

    public List<Float> drawSmooth(float[] pts, Float tension, Boolean isClosed, Integer numOfSegments) {
        tension = (tension != null) ? tension : 0.5f;
        isClosed = (isClosed != null) ? isClosed : false;
        numOfSegments = numOfSegments != null ? numOfSegments : 16;
        List<Float> _pts = new ArrayList<Float>();
        List<Float> res = new ArrayList<Float>();
        float x, y, t1x, t2x, t1y, t2y, c1, c2, c3, c4, st, t;
        for (int j = 0; j < pts.length; j++) {
            _pts.add(pts[j]);
        }
        if (isClosed) {
            _pts.add(0, pts[pts.length - 1]);
            _pts.add(0, pts[pts.length - 2]);
            _pts.add(0, pts[pts.length - 1]);
            _pts.add(0, pts[pts.length - 2]);
            _pts.add(pts[0]);
            _pts.add(pts[1]);
        } else {
            _pts.add(0, pts[1]);
            _pts.add(0, pts[0]);
            _pts.add(pts[pts.length - 2]);
            _pts.add(pts[pts.length - 1]);
        }

        for (int i = 2; i < (_pts.size() - 4); i += 2) {
            for (t = 0; t <= numOfSegments; t++) {

                t1x = (_pts.get(i + 2) - _pts.get(i - 2)) * tension;
                t2x = (_pts.get(i + 4) - _pts.get(i)) * tension;

                t1y = (_pts.get(i + 3) - _pts.get(i - 1)) * tension;
                t2y = (_pts.get(i + 5) - _pts.get(i + 1)) * tension;

                st = t / numOfSegments;

                c1 = (float) (2 * Math.pow(st, 3) - 3 * Math.pow(st, 2) + 1);
                c2 = (float) (-1 * (2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2));
                c3 = (float) (Math.pow(st, 3) - 2 * Math.pow(st, 2) + st);
                c4 = (float) (Math.pow(st, 3) - Math.pow(st, 2));

                x = c1 * _pts.get(i) + c2 * _pts.get(i + 2) + c3 * t1x + c4 * t2x;
                y = c1 * _pts.get(i + 1) + c2 * _pts.get(i + 3) + c3 * t1y + c4 * t2y;

                res.add(x);
                res.add(y);
            }
        }

        return res;
    }

    private int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public void setBg(boolean bg) {
        this.bg = bg;
    }
}
